<?php

// +----------------------------------------------------------------------
// | MC PHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author kobe [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------


namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\service\ApiReturn;
use think\facade\Db;
use app\service\order\Recharge as RechargeService;

/**
 * 订单接口
 */
class Order extends Base
{
    /**
     * 统一下单
     * 请注意，内部直接抛出异常
     * @param  array  $data [description]
     * @param  array  $user [description]
     * @return [type]       [description]
     */
    public function unifiedOrder($data=[],$user=[])
    {
        // 启动事务
        Db::startTrans();
        try {
            switch($data['order_type']) {
                case 1:
                    $order = \app\admin\model\OrderRecharge::createRechargeOrder($data, $user);
                    break;
                case 2:
                    $order = \app\admin\model\OrderVip::createVipOrder($data, $user);
                    break;
                case 3:
                    
                    break;
                default:
                    Db::rollback();
                    return ApiReturn::r(0, null,'不支持该下单类型');
                    break;
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ApiReturn::r(0,$e->getTrace(),'下单失败');
        }
        return ApiReturn::r(1, $order,'下单成功，请支付');
    }

}
